package guoanmaker.operator.business.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.aliyun.oss.OSSClient;

import guoanmaker.operator.business.model.OperatorEventDetails;
import guoanmaker.operator.business.modelnobind.EventDetailsMove;
import guoanmaker.operator.business.service.OperatorEventDetailsServiceInterface;
import guoanmaker.operator.helper.common.AjaxJson;
import guoanmaker.operator.helper.common.abstractobj.ApiController;
import guoanmaker.operator.helper.common.util.OSSUnitHelper;
import io.swagger.annotations.ApiOperation;
/**
 * @author wanglan 
 * @ClassName: OperatorEventDetailsController
 * @Description: OperatorEventDetailsController 
 * @date 2017/8/11 下午15:40
 */
@RestController
@RequestMapping(ApiController.OPERATOR_EVEN)
public class OperatorEventDetailsController {
	private final OperatorEventDetailsServiceInterface operatorEventDetailsServiceInterface;

	public OperatorEventDetailsController(
			OperatorEventDetailsServiceInterface operatorEventDetailsServiceInterface) {
		this.operatorEventDetailsServiceInterface = operatorEventDetailsServiceInterface;
	}

	/**
	 * @Title: getOperatorEventDetailsAll
	 * @Description: 查询所有活动
	 * @param:OperatorEventDetails
	 * @return AjaxJson @throws 
	 */
	@ApiOperation(value = "查询所有活动", notes = "根据EventType字段查询")
	@PostMapping(value = "getOperatorEventDetailsAll")
	public AjaxJson getOperatorEventDetailsAll(@RequestParam Integer EventType,
			@RequestParam(value = "page", defaultValue = "0") Integer page,
			@RequestParam(value = "size", defaultValue = "10") Integer size) {
		AjaxJson ajaxJson=new AjaxJson();
		return ajaxJson.setSuccess(true).setStatus(200).setData(operatorEventDetailsServiceInterface.getAllOperatorEventDetails(EventType, page, size));
	}
	/**
	 * @Title: getOperatorEventDetailsAll
	 * @Description: 查询所有活动
	 * @param:OperatorEventDetails
	 * @return AjaxJson @throws 
	 */
	@ApiOperation(value = "查询所有活动App,IOS,PC", notes = "根据EventType字段查询")
	@PostMapping(value = "getAppAndIosAndPcOperatorEventDetailsAll")
	public AjaxJson getAppAndIosAndPcOperatorEventDetailsAll(@RequestParam Integer EventType,
			@RequestParam(value = "page", defaultValue = "0") Integer page,
			@RequestParam(value = "size", defaultValue = "10") Integer size) {
		AjaxJson ajaxJson=new AjaxJson();
		return ajaxJson.setSuccess(true).setStatus(200).setData(operatorEventDetailsServiceInterface.getAppAndIosAndPcAllOperatorEventDetails(EventType, page, size));
	}


	/**
	 * 
	 * @Title: getSelectEvent
	 * @Description: 查看活动
	 * @param:OperatorEventDetails
	 * @return AjaxJson     @throws 
	 */
	@ApiOperation(value = "查看活动", notes = "根据ID查看活动")
	@PostMapping(value = "getSelectEvent")
	public AjaxJson getSelectEvent(@RequestParam String id) {
		AjaxJson ajaxJson=new AjaxJson();
		return ajaxJson.setSuccess(true).setStatus(200).setData(operatorEventDetailsServiceInterface.getSelectEvent(id));
	}

	/**
	 *  
	 * 
	 * @Title: getSelectEventAll
	 * @Description: 查看所有活动不分上下线
	 * @param: OperatorEventDetails
	 * @return AjaxJson     @throws 
	 */
	@ApiOperation(value = "查看所有活动不分上下线", notes = "查看活动")
	@PostMapping(value = "getSelectEventAll")
	public AjaxJson getSelectEventAll() {
		AjaxJson ajaxJson=new AjaxJson();
		return ajaxJson.setSuccess(true).setStatus(200).setData(operatorEventDetailsServiceInterface.getSelectEventAll());
	}
	/**
	 *  
	 * 
	 * @Title: getSelectEventAll
	 * @Description: 查看所有活动不分上下线
	 * @param: OperatorEventDetails
	 * @return AjaxJson     @throws 
	 */
	
	 @ApiOperation(value = "获取活动报名人详情", notes = "查看报名人详情资料")
	 @PostMapping(value = "getObtainDetailsOfApplicants") 
	 public AjaxJson getObtainDetailsOfApplicants(@RequestParam String id,
			 @RequestParam String activityTypeId,
			@RequestParam(value = "page", defaultValue = "0") Integer page,
			@RequestParam(value = "size", defaultValue = "10") Integer size){
		 AjaxJson ajaxJson=new AjaxJson();
		 return ajaxJson.setData(operatorEventDetailsServiceInterface.getEventEnrollment(id,activityTypeId,page,size)).setStatus(200);
	 }
	 
	/**
	 *  
	 * 
	 * @Title: adoptIdModify
	 * @Description: 编辑修改活动
	 * @param: OperatorEventDetails
	 * @return AjaxJson     @throws 
	 */
	@ApiOperation(value = "编辑修改活动", notes = "根据ID修改编辑活动")
	@PostMapping(value = "adoptIdModify")
	public AjaxJson adoptIdModify(@RequestBody OperatorEventDetails OperatorEventDetails) {
		AjaxJson ajaxJson=new AjaxJson();
		return ajaxJson.setSuccess(true).setData(operatorEventDetailsServiceInterface.saveNewOperatorEventDetails(OperatorEventDetails));
	}

	/**
	 *  
	 * 
	 * @Title: getselectByEventId
	 * @Description: 通过活动类型查询活动详情
	 * @param:
	 * @return AjaxJson     @throws 
	 */
	@ApiOperation(value = "通过活动类型查询活动详情", notes = "根据活动ID查询活动")
	@PostMapping(value = "getselectByEventId")
	public AjaxJson selectByEventId(@RequestParam String id,
			@RequestParam(value = "page", defaultValue = "0") Integer page,
			@RequestParam(value = "size", defaultValue = "10") Integer size) {
		AjaxJson ajaxJson=new AjaxJson();
		return ajaxJson.setData(operatorEventDetailsServiceInterface.getselectByEventId(id, page, size));
	}

	/**
	 *  
	 * 
	 * @Title: deleteEvet
	 * @Description: 删除活动
	 * @param: OperatorEventDetails
	 * @return AjaxJson     @throws 
	 */
	@ApiOperation(value = "删除活动", notes = "根据ID删除活动")
	@PostMapping(value = "deleteEvet")
	public AjaxJson deleteEvet(@RequestParam String id) {
		AjaxJson ajaxJson=new AjaxJson();
		operatorEventDetailsServiceInterface.delete(id);
		return ajaxJson.setStatus(200).setMsg("删除成功");
	}

	/**
	 *  
	 * 
	 * @Title: endEvent
	 * @Description: 结束活动
	 * @param: OperatorEventDetails
	 * @return AjaxJson     @throws 
	 */
	@ApiOperation(value = "结束活动", notes = "通过状态值结束活动")
	@PostMapping(value = "endEvent")
	public AjaxJson endEvent(@RequestParam String id) {
		AjaxJson ajaxJson=new AjaxJson();
		operatorEventDetailsServiceInterface.updateState(new OperatorEventDetails(), id);
		return ajaxJson.setStatus(200);
	}

	/**
	 *  
	 * 
	 * @Title: accordingToLikeFuzzyQuery
	 * @Description: 根据活动标题模糊查询
	 * @param: OperatorEventDetails
	 * @return AjaxJson @throws 
	 */
	@ApiOperation(value = "根据活动标题模糊查询", notes = "模糊查询")
	@PostMapping("accordingToLikeFuzzyQuery")
	public AjaxJson accordingToLikeFuzzyQuery(@RequestParam String eventTitle, @RequestParam Integer EventType,
			@RequestParam(value = "page", defaultValue = "0") Integer page,
			@RequestParam(value = "size", defaultValue = "10") Integer size) {
		AjaxJson ajaxJson=new AjaxJson();
		return ajaxJson.setSuccess(true).setStatus(200)
				.setData(operatorEventDetailsServiceInterface.LikeSelect(eventTitle, EventType, page, size));
	}

	/**
	 *  
	 * 
	 * @Title: accordingToQuery
	 * @Description: 根据多条件查询查询
	 * @param: OperatorEventDetails
	 * @return AjaxJson     @throws 
	 */
	@ApiOperation(value = "根据多条件查询查询", notes = "查询")
	@PostMapping("accordingToQuery")
	public AjaxJson accordingToQuery(@RequestBody OperatorEventDetails operatorEventDetails) {
		AjaxJson ajaxJson=new AjaxJson();
		Map<String, Object> multipleConditionQuery = operatorEventDetailsServiceInterface.multipleConditionQuery(
				operatorEventDetails.getEventType(), operatorEventDetails.getEventTitle(),
				operatorEventDetails.getState(), operatorEventDetails.getFounder(), operatorEventDetails.getIsMoney(),
				operatorEventDetails.getStratTime(), operatorEventDetails.getEndTime(), operatorEventDetails.getPage(),
				operatorEventDetails.getSize());

		return ajaxJson.setSuccess(true).setStatus(200).setData(multipleConditionQuery);
	}
	

	/**
	 * @Title: saveEvent
	 * @Description: 保存活动详情
	 * @param: OperatorEventDetails
	 * @return AjaxJson     @throws 
	 */
	@ApiOperation(value = "保存活动详情", notes = "保存")
	@PostMapping("saveEvent")
	public AjaxJson saveEvent(@RequestBody OperatorEventDetails operatorEventDetails) {
		return operatorEventDetailsServiceInterface.saveEvent(operatorEventDetails);
	}
	
	@ApiOperation(value="导出活动报名人详情",notes="导出")
	@PostMapping("exportEventDetailsOfApplicants")
	public AjaxJson exportEventDetailsOfApplicants(@RequestParam String id,@RequestParam String activityTypeId,@RequestParam String maxTime,@RequestParam String minTime){
		AjaxJson ajaxJson=new AjaxJson();
		return ajaxJson.setData(operatorEventDetailsServiceInterface.exportEventDetailsOfApplicants(id,activityTypeId,maxTime,minTime));
	}
	
	@ApiOperation(value="查询活动标题是否重复",notes="查询活动标题是否重复")
	@PostMapping("theQueryActivityTitleIsRepeated")
	public AjaxJson theQueryActivityTitleIsRepeated(@RequestParam String eventTitle){
		AjaxJson ajaxJson=new AjaxJson();
		return ajaxJson.setData(operatorEventDetailsServiceInterface.theQueryActivityTitleIsRepeated(eventTitle));
	}
	/**
	 * @Title: UploadLuckDraw
	 * @Description: 活动图片上传接口
	 * @param: OperatorLuckdrawController
	 * @return AjaxJson     @throws 
	 */
	@ApiOperation(value = "活动海报图片上传接口", notes = "活动图片上传接口")
	@PostMapping(value = "/UploadEventDetails")
	public AjaxJson UploadEventDetails(MultipartFile upfile, HttpServletResponse response) {
		AjaxJson ajaxJson=new AjaxJson();
		if (upfile != null) {
			OSSClient client = OSSUnitHelper.getOSSClient();
			String FileName = new Date().getTime() + (int) ((Math.random() * 9 + 1) * 100000)
					+ upfile.getOriginalFilename().substring(upfile.getOriginalFilename().indexOf("."));
			System.out.println(FileName);
			OSSUnitHelper.uploadObjectOSS(client, upfile, ApiController.EVENTDETAILS_URL, FileName);
			String url = OSSUnitHelper.getUrl(client, ApiController.EVENTDETAILS_URL, FileName);
			url = url.replace("http://", "https://");
			if (url != null && !url.equals("")) {
				ajaxJson.setSuccess(true).setStatus(200).setData(url);
				response.setContentType(url);
			}
		}
		return ajaxJson;
	}

	@ApiOperation(value = "活动详情图片上传接口", notes = "活动图片上传接口")
	@PostMapping(value = "/UploadEventDetailspic")
	public AjaxJson UploadEventDetailspic(MultipartFile upfile, HttpServletResponse response) {
		AjaxJson ajaxJson=new AjaxJson();
		if (upfile != null) {
			OSSClient client = OSSUnitHelper.getOSSClient();
			String FileName = new Date().getTime() + (int) ((Math.random() * 9 + 1) * 100000)
					+ upfile.getOriginalFilename().substring(upfile.getOriginalFilename().indexOf("."));
			System.out.println(FileName);
			OSSUnitHelper.uploadObjectOSS(client, upfile, ApiController.EVENTDETAILSPIC_URL, FileName);
			String url = OSSUnitHelper.getUrl(client, ApiController.EVENTDETAILSPIC_URL, FileName);
			url = url.replace("http://", "https://");
			if (url != null && !url.equals("")) {
				ajaxJson.setSuccess(true).setStatus(200).setData(url);
				response.setContentType(url);
			}
		}
		return ajaxJson;
	}

	/**
	 * @Title: selectSecckillEventDetails
	 * @Description: 查询最近时间秒杀活动
	 * @param:OperatorEventDetails
	 * @return AjaxJson @throws 
	 */
	@ApiOperation(value = "查询最近时间秒杀活动", notes = "查询最近时间秒杀活动")
	@PostMapping(value = "selectSecckillEventDetails")
	public AjaxJson selectSecckillEventDetails() {
		AjaxJson ajaxJson=new AjaxJson();
		List<EventDetailsMove> list=operatorEventDetailsServiceInterface.selectSecckillEventDetails();
		return  ajaxJson.setSuccess(true).setStatus(200).setData(list);
	}
}